package form.ui
{
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import form.dispatchers.FormElementDispatcher;
	import form.events.FormElementEvent;
	import form.interfaces.IFormElement;
	import form.ui.core.FMSprite;

	public class Label extends FMSprite implements IFormElement
	{
		public static const FONT_COLOR:int = 0x333333;
		
		public static const FONT_FAMILY:String = "Verdana";
		public static const FONT_SIZE:int = 12;
		
		protected var textfield:TextField;
		protected var rawText:String;
		
		private var _formName:String;
		private var _maxLength:uint = 10;
		
		public function Label()
		{
			super();
			
			this.minWidth = 32;
			this.minHeight = 18;
			
			this.registerStyle("color");
			
			textfield = new TextField;
			textfield.height = minHeight;
			textfield.width = minWidth;
			textfield.selectable = false;
			//textfield.autoSize = TextFieldAutoSize.LEFT;
			textfield.defaultTextFormat = new TextFormat(FONT_FAMILY, FONT_SIZE, FONT_COLOR);
			addChild(textfield);
			
			var e:FormElementEvent = new FormElementEvent(FormElementEvent.INITIALIZED);
			e.element = this;
			
			FormElementDispatcher.instance.dispatchEvent(e);
		}
		
		public function set formName(value:String):void
		{
			_formName = value;
		}
		
		public function get formName():String
		{
			return _formName;
		}
		
		public function set formValue(value:String):void
		{
			rawText = value;
			value = textfield.text.length < maxLength ? value : value.substr(0, maxLength - 3) + "...";
			textfield.text = value;
		}
		
		public function set autoSize(value:String):void
		{
			textfield.autoSize = value;
		}
		public function get autoSize():String
		{
			return textfield.autoSize;
		}
		
		public function get formValue():String
		{
			return rawText;
		}
		
		public function set maxLength(value:uint):void
		{
			_maxLength = value;
		}
		
		public function get maxLength():uint
		{
			return _maxLength;
		}
		
		public function set textAlign(value:String):void
		{
			var format:TextFormat = textfield.defaultTextFormat;
			format.align = value;
			textfield.defaultTextFormat = format;
			update();
		}
		
		public function get textAlign():String
		{
			return textfield.defaultTextFormat.align;
		}
		
		public function set textSize(value:uint):void
		{
			var format:TextFormat = textfield.defaultTextFormat;
			format.size = value;
			textfield.defaultTextFormat = format;
			textfield.height = value < minHeight ? minHeight : value + 2; 
			update();
		}
		
		public function get textSize():uint
		{
			return uint(textfield.defaultTextFormat.size);
		}
				
		public function set textBold(value:Boolean):void
		{
			var format:TextFormat = textfield.defaultTextFormat;
			format.bold = value;
			textfield.defaultTextFormat = format;
			update();
		}
		
		public function get textBold():Boolean
		{
			return Boolean(textfield.defaultTextFormat.bold);
		}
		
		public function get color():uint
		{
			return fontColor;
		}
		public function set color(value:uint):void
		{
			fontColor = value;
		}
		
		public function get fontColor():uint
		{
			return textfield.textColor;
		}
		public function set fontColor(value:uint):void
		{
			textfield.textColor = value;			
		}
		
		override public function set width(value:Number):void
		{
			textfield.width = value;
			//super.width = value;
		}
		override public function get width():Number
		{
			return textfield ? textfield.width : super.width;
		}
				
		override public function get height():Number
		{
			return super.height ? super.height : this.minHeight;
		}
		
		private function update():void
		{
			if(rawText)textfield.text = rawText;
		}
		
		public function enable():void
		{
			
		}
		
		public function disable():void
		{
		}
		
	}
}